﻿@model PagedList.IPagedList<TestOnlineProject.Models.Entities.DiscussionModel>
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{
    if (!String.IsNullOrEmpty(ViewBag.Msg))
    {
        <script> alert("@ViewBag.Msg")</script>
    }
}
<script>
    $(document).ready(function() {
        $("button[name=Create]").click(function() {
            window.location = '@Url.Action("Create", "Discussion", new { subjectId = @ViewBag.Subject.SubjectId })';
        });

        // Select all.
        $("#select_all").click(function() {
            $(".is_selected").attr('checked', true);
        });

        // Unselect all.
        $("#unselect_all").click(function() {
            $(".is_selected").attr('checked', false);
        });

        $("#deactivate").click(function() {
            var val = "";
            $(':checkbox:checked').each(function(i) {
                val += $(this).val() + "-";
            });
            if (val == "") {
                alert("You must select at least one Discussion!");
            } else {
                var con = confirm("Are you sure You want to deactivate this Discussion(s)?");
                if (con == true) {
                    $.ajax({
                        url: "@Url.Content("~/Discussion/Deactivate")?ids=" + val,
                        type: 'GET',
                        success: function(result) {
                            if (result != null) {
                                alert(result);
                                $.ajax({
                                    url: "@Url.Content("~/Discussion/Index")?page=@ViewBag.Page&subjectId=@ViewBag.Subject.SubjectId&keyWord=@ViewBag.KeyWord",
                                    type: 'GET',
                                    success: function(data) {
                                        $("#unobtrusive").html(data);
                                        $("button").removeClass('active');
                                        $("#all").addClass('active');
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

        $("#activate").click(function() {
            var val = "";
            $(':checkbox:checked').each(function(i) {
                val += $(this).val() + "-";
            });
            if (val == "") {
                alert("You must select at least one Discussion!");
            } else {
                var con = confirm("Are you sure You want to Activate this Discussion(s)?");
                if (con == true) {
                    $.ajax({
                        url: "@Url.Content("~/Discussion/Activate")?ids=" + val,
                        type: 'GET',
                        success: function(result) {
                            if (result != null) {
                                alert(result);
                                $.ajax({
                                    url: "@Url.Content("~/Discussion/Index")?page=@ViewBag.Page&subjectId=@ViewBag.Subject.SubjectId&keyWord=@ViewBag.KeyWord",
                                    type: 'GET',
                                    success: function(data) {
                                        $("#unobtrusive").html(data);
                                        $("button").removeClass('active');
                                        $("#all").addClass('active');
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

        $("#delete").click(function() {
            var val = "";
            $(':checkbox:checked').each(function(i) {
                val += $(this).val() + "-";
            });
            if (val == "") {
                alert("You must select at least one Discussion!");
            } else {
                var con = confirm("Are you sure You want to Delete this Discussion(s)?");
                if (con == true) {
                    $.ajax({
                        url: "@Url.Content("~/Discussion/Delete")?ids=" + val,
                        type: 'GET',
                        success: function(result) {
                            if (result != null) {
                                alert(result);
                                $.ajax({
                                    url: "@Url.Content("~/Discussion/Index")?page=@ViewBag.Page&subjectId=@ViewBag.Subject.SubjectId&keyWord=@ViewBag.KeyWord",
                                    type: 'GET',
                                    success: function(data) {
                                        $("#unobtrusive").html(data);
                                        $("button").removeClass('active');
                                        $("#all").addClass('active');
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    });
    
    function active(element, id) {
        switch (id) {
        case "my":
            $.ajax({
                url: "@Url.Content("~/Discussion/Index")?subjectId=@ViewBag.Subject.SubjectId&userId=@HttpContext.Current.User.Identity.Name&status=My",
                type: 'GET',
                success: function(data) {
                    $("#unobtrusive").html(data);
                }
            });
            break;
        case "all":
            $.ajax({
                url: "@Url.Content("~/Discussion/Index")?subjectId=@ViewBag.Subject.SubjectId",
                type: 'GET',
                success: function(data) {
                    $("#unobtrusive").html(data);
                }
            });
            break;
        case "showDeactivate":
            $.ajax({
                url: "@Url.Content("~/Discussion/Index")?subjectId=@ViewBag.Subject.SubjectId&status=False",
                type: 'GET',
                success: function(data) {
                    $("#unobtrusive").html(data);
                }
            });
            break;
        default:
            var keyWord = $("#txtsearch").val();
            $.ajax({
                url: "@Url.Content("~/Discussion/Index")?subjectId=@ViewBag.Subject.SubjectId&keyWord=" + keyWord,
                type: 'GET',
                success: function(data) {
                    $("#unobtrusive").html(data);
                }
            });
        }

        $("button").removeClass('active');
        if (id == "btnsearch") {
            $("#spansearch").show();
            $("#search").addClass('active');
        } else {
            $(element).addClass('active');
            $("#spansearch").hide();
        }
    }
</script>
<div class="site_map">
    @Html.ActionLink("Home", "Index", "Home", new { Area = "" }, new { })
    / @Html.ActionLink("Subjects", "Index", "Subject", new { }, null)
    / @Html.ActionLink((string) @ViewBag.Subject.Code, "Details", "Subject", new { id = @ViewBag.Subject.SubjectId }, null)
    / Discussions
</div>
<br />
<div class="main_nav">
    <span>
        <a href="@Url.Content("~/Subject/Details")?id=@ViewBag.Subject.SubjectId">
            <img src="@Url.Content("~/Images/back button.png")" />
        </a>
    </span>
    <span style="float: right">
        @if (HttpContext.Current.User.IsInRole("1"))
        {
            @Html.ActionLink("Participants", "Index", "Participant", new { subjectId = @ViewBag.Subject.SubjectId }, new { }) <span>|</span>
        }
        @Html.ActionLink("Sections", "Index", "Section", new { subjectId = @ViewBag.Subject.SubjectId }, new { })
        |
        @Html.ActionLink("Questions", "Index", "Question", new { subjectId = @ViewBag.Subject.SubjectId }, new { })
        |
        @Html.ActionLink("Tests", "Index", "Test", new { subjectId = @ViewBag.Subject.SubjectId }, new { })
    </span>
</div>
<br />
<div>
    <div class="search_content">
        <input style="margin: 5px 0 5px 5px" type="text" id="txtsearch" name="txtsearch" value="" placeholder="Discussion's Title"/>
        <button style="margin: 5px 5px 5px 0" type="button" value="Search" id="btnsearch" class="btn" onclick="active($(this), this.id)">Search</button>
    </div>
    <br />
    <div>
        <button class="btn" type="button" name="Create">Create Discussion</button>
    </div>
    <br />
    <table class="table table-bordered" style="width: 100%">
        <tr style="background-color: #F5F5F5">
            <td>
                <span style="padding-left: 5px">
                    <button id="all" class="btn btn-small active" onclick="active($(this), this.id)" style="width: 120px">
                        <b>All Discussions</b>
                    </button>
                </span>
                <span style="padding-left: 5px">
                    <button id="my" class="btn btn-small" style="width: 120px" onclick="active($(this), this.id)">
                        <b>My Discussions</b>
                    </button>
                </span>
                @if (HttpContext.Current.User.IsInRole("1"))
                {
                    <span style="padding-left: 5px">
                        <button id="showDeactivate" class="btn btn-small" style="width: 110px" onclick="active($(this), this.id)">
                            <b>Deactivate</b>
                        </button>
                    </span>
                }
                <span id="spansearch" style="padding-left: 5px; display: none">
                    <button id="search" class="btn btn-small" style="width: 120px"><b>Result</b></button>
                </span>
                @if (HttpContext.Current.User.IsInRole("1"))
                {
                    <div class="btn-group" style="float: right; padding-right: 5px">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu pull-right">
                            <li><button class="btn btn-link" id="select_all">Select All</button></li>
                            <li><button class="btn btn-link" id="unselect_all">Unselect All</button></li>
                            <li><button class="btn btn-link" id="activate">Activate</button></li>
                            <li><button class="btn btn-link" id="deactivate">Deactivate</button></li>
                            <li><button class="btn btn-link" id="delete">Delete</button></li>
                        </ul>
                    </div>
                }
            </td>
        </tr>
    </table>
    <div id="unobtrusive">
        @Html.Partial("_List")
    </div>
</div>